/**************************************************************
*  Apple LLVM version 10.0.0 (clang-1000.10.43.1)             *
*  Copyright (C) 2021 Xuan-Yu-San 307100890@qq.com.           *
*                                                             *
*  This file is part of little-nginx.                         *
*  Little-NGINX is a castration version NGINX for learning    *
*  HTTP Message.                                              *
*                                                             *
*  @file     Server.h                                         *
*  @github   https://gitee.com/xuanyusan/little_nginx.git     *
*  @license  MIT License (MIT)                                *
**************************************************************/

#ifndef CLIENT_HPP
#define CLIENT_HPP
#include "Logger.hpp"
#include "Socket.hpp"
#include "NginxConfig.hpp"

class NGINXException: public exception {
private:
    const char* error;
public:
    NGINXException(const char* error):exception(), error(error) {}
    const char * what () const throw () {
        return this->error;
    }
};

class Client: public Socket, public Connect {
public:
    Client(int, string);
    Client(NginxConfig);
    ~Client();
};

Client::Client(int socket_port, string socket_host):Socket(socket_port, socket_host), Connect(-1) {
    this->reset_connect_id(this->socket_id);
    this->sockaddr.sin_addr.s_addr = inet_addr(this->socket_host.c_str());
    if (connect(this->socket_id, (struct sockaddr *)&this->sockaddr, sizeof(this->sockaddr)) < 0) {
        throw NGINXException("Connect error");
    }
}

Client::Client(NginxConfig nginx_config):Socket(nginx_config), Connect(-1) {
    this->reset_connect_id(this->socket_id);
    this->sockaddr.sin_addr.s_addr = inet_addr(this->socket_host.c_str());
    if (connect(this->socket_id, (struct sockaddr *)&this->sockaddr, sizeof(this->sockaddr)) < 0) {
        perror("Connect error");
        exit(1);
    }
}

Client::~Client() {}
#endif